package mongodb;

import org.apache.commons.beanutils.BeanMap;
import org.apache.commons.beanutils.BeanUtils;

import java.lang.reflect.Field;
import java.lang.reflect.Modifier;
import java.util.HashMap;
import java.util.Map;
import java.util.logging.Logger;


public class MapBeanConverUtil {

    private static Logger log = Logger.getLogger("MapBeanConverUtil");
    public static Map<String, Object> PO2Map(Object o) throws Exception{
        Map<String, Object> map = new HashMap<String, Object>();
        Field[] fields = null;
        String className = o.getClass().getSimpleName();
        fields = o.getClass().getDeclaredFields();
        for (Field field : fields) {
            field.setAccessible(true);
            String key = field.getName();
            Object value = field.get(o);
            map.put(key, value);
        }
        return map;
    }


    public static Object map2PO(Map<String,Object> map,Object o) throws Exception{
        if (!map.isEmpty()) {
            for (String k : map.keySet()) {
                Object v = "";
                if (!k.isEmpty()) {
                    v = map.get(k);
                }
                Field[] fields = null;
                fields = o.getClass().getDeclaredFields();
                String className = o.getClass().getSimpleName();
                log.info("类："+o.getClass().getName());
                log.info("***map转"+className+"开始****");
                for (Field field : fields) {
                    int mod = field.getModifiers();
                    if(Modifier.isStatic(mod) || Modifier.isFinal(mod)){
                        continue;
                    }
                    if (field.getName().equals(k)) {
                        field.setAccessible(true);
                        field.set(o, v);
                        log.info("key："+k+"value:"+v);
                    }

                }
                log.info("***map转"+className+"结束****");
            }
        }
        return o;
    }

    public static void transMap2Bean(Map<String, Object> map, Object obj) {
        if (map == null || obj == null) {
            return;
        }
        try {
            BeanUtils.populate(obj, map);
        } catch (Exception e) {
            System.out.println("transMap2Bean2 Error " + e);
        }
    }

    public static BeanMap transBean2Map(Object obj) {
        if (obj == null) {
            return null;
        }
        try {
            return new BeanMap(obj);
        } catch (Exception e) {
            System.out.println("transBean2Map Error " + e);
        }
        return null;
    }


    public static  void main(String[] args) throws Exception {
        MongoDB mongoDB=new MongoDB();
        mongoDB.setId(111);
        mongoDB.setDbName("name1");
        TestBean testBean=new TestBean();
        testBean.setId(222);
        testBean.setName("name222");
        testBean.setMongoDB(mongoDB);
        Map map=MapBeanConverUtil.transBean2Map(testBean);
        System.out.println(map);
        TestBean testBean1=new TestBean();
        MapBeanConverUtil.transMap2Bean(map,testBean1);
        System.out.println(testBean1);
    }
}
